﻿/*
 *  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
 *  Copyright (C)  2012. Ian Quigley
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Routing;

namespace ChgDoc.Extension
{
    public static class StringExtension
    {
        public static void SplitInto(this string str, char c, out string one, out string two)
        {
            string[] parts = (!string.IsNullOrEmpty(str)) ? str.Split(c) : new string[] { string.Empty };
            one = parts[0];
            two = (parts.Length > 1) ? parts[1] : string.Empty;            
        }

        public static void SplitInto(this string str, char c, out string one, out string two, out string three)
        {
            string[] parts = (!string.IsNullOrEmpty(str)) ? str.Split(c) : new string[] { string.Empty };
            one = parts[0];
            two = (parts.Length > 1) ? parts[1] : string.Empty;
            three = (parts.Length > 2) ? parts[2] : string.Empty;
        }

        public static IEnumerable<string> FromMagicString(this string magic, params string[] parameters)
        {
            int open, close, last;
            open = 0;
            last = 0;
            

            while (open < magic.Length)
            {
                open = magic.IndexOf("{", open);
                if (open == -1)
                {
                    yield return magic.Substring(last);
                    yield break;
                }

                close = magic.IndexOf("}", open);
                if (close == -1)
                {
                    yield return magic.Substring(open);
                    yield break;
                }

                if (open != last)
                    yield return magic.Substring(last, open - last);

                last = close+1;

                string part = magic.Substring(open + 1, close - open - 1);
                open = close + 1;

                string option = string.Empty;
                if (part.IndexOf(":") != -1)
                {
                    string a = part.Substring(0, part.IndexOf(":"));
                    option = part.Substring(part.IndexOf(":") + 1);
                    part = a;
                }

                switch (part.ToLower())
                {
                    case "qs":
                        yield return HttpContext.Current.Request.QueryString[option];
                        break;

                    case "route":
                        yield return HttpContext.Current.Request.RequestContext.RouteData.Values[option].ToString();
                        break;

                    case "param":
                        yield return parameters[ int.Parse( option ) ];
                        break;
                        
                    default:
                        yield return part;
                        break;
                }
            }
        }

        public static string PickRandomly(this string str, int length)
        {
            StringBuilder result = new StringBuilder();
            Random r = new Random();

            while (length-- > 0)
                result.Append(str[r.Next(0, str.Length)]);

            return result.ToString() ;
        }
     
    }
}
